{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "45d65bcc-979a-4a3b-b6f9-77a5e8276091",
   "metadata": {},
   "source": [
    "# 任务\n",
    "手写 `Cart` 决策树"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cd838be-98d9-4cda-a4aa-45c747b8c3b8",
   "metadata": {},
   "source": [
    "### 导入必要的包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9955a328-c84a-402a-92af-a1931b6a6eb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8ce916e-ecd7-40bd-9a88-3a0de070f3c0",
   "metadata": {},
   "source": [
    "### 导入自己写的 `Cart` 决策树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bfb0f579-a370-4f24-b7ed-63e800e06a95",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ModelDefination.CARTDecisionTree import CARTDecisionTree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "68057fc3-f17b-4926-87f0-42ec36ae2e8d",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'CARTDecisionTree' object has no attribute 'split'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 15\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[38;5;66;03m# 训练CART决策树\u001b[39;00m\n\u001b[0;32m     14\u001b[0m tree \u001b[38;5;241m=\u001b[39m CARTDecisionTree(max_depth\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m)\n\u001b[1;32m---> 15\u001b[0m tree\u001b[38;5;241m.\u001b[39mfit(X_train, y_train)\n\u001b[0;32m     17\u001b[0m \u001b[38;5;66;03m# 预测\u001b[39;00m\n\u001b[0;32m     18\u001b[0m y_pred \u001b[38;5;241m=\u001b[39m tree\u001b[38;5;241m.\u001b[39mpredict(X_test)\n",
      "File \u001b[1;32mD:\\Users\\Huanghq\\Desktop\\ML_homework\\Spaceship_Titanic\\ModelDefination\\CARTDecisionTree.py:25\u001b[0m, in \u001b[0;36mfit\u001b[1;34m(self, X, y)\u001b[0m\n\u001b[0;32m     23\u001b[0m # 训练模型\n\u001b[0;32m     24\u001b[0m def fit(self, X, y):\n\u001b[1;32m---> 25\u001b[0m     self.tree = self.grow_tree(X, y)\n\u001b[0;32m     26\u001b[0m \n\u001b[0;32m     27\u001b[0m # 使用模型进行预测\n",
      "File \u001b[1;32mD:\\Users\\Huanghq\\Desktop\\ML_homework\\Spaceship_Titanic\\ModelDefination\\CARTDecisionTree.py:93\u001b[0m, in \u001b[0;36mgrow_tree\u001b[1;34m(self, X, y, depth)\u001b[0m\n\u001b[0;32m     91\u001b[0m idx, thr = self.best_split(X, y)\n\u001b[0;32m     92\u001b[0m if idx is not None:\n\u001b[1;32m---> 93\u001b[0m     X_left, X_right, y_left, y_right = self.split(X, y, idx, thr)\n\u001b[0;32m     94\u001b[0m     node.feature_index = idx\n\u001b[0;32m     95\u001b[0m     node.threshold = thr\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'CARTDecisionTree' object has no attribute 'split'"
     ]
    }
   ],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "# 加载数据集\n",
    "iris = load_iris()\n",
    "X = iris.data\n",
    "y = iris.target\n",
    "\n",
    "# 划分训练集和测试集\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)\n",
    "\n",
    "# 训练CART决策树\n",
    "tree = CARTDecisionTree(max_depth=3)\n",
    "tree.fit(X_train, y_train)\n",
    "\n",
    "# 预测\n",
    "y_pred = tree.predict(X_test)\n",
    "\n",
    "# 评估\n",
    "accuracy = accuracy_score(y_test, y_pred)\n",
    "print(f\"模型准确率: {accuracy:.2f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ac6fc512-3514-43bb-9b51-81662de0b0e7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
